package com.brush.web.controller.config;

import com.alibaba.excel.EasyExcel;
import com.brush.common.annotation.Log;
import com.brush.common.core.controller.BaseController;
import com.brush.common.core.domain.R;
import com.brush.common.enums.BusinessType;
import com.brush.system.domain.req.sysconfig.SysConfigAddReq;
import com.brush.system.domain.req.sysconfig.SysConfigEditReq;
import com.brush.system.domain.req.sysconfig.SysConfigQueryReq;
import com.brush.system.domain.resp.sysconfig.SysConfigResp;
import com.brush.system.service.SysConfigService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Api(tags = "后台-参数配置Api")
@RestController
@RequestMapping(value = "/admin/config")
public class SysConfigController extends BaseController {

    @Autowired
    private SysConfigService sysConfigService;


    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/list")
    public R<PageInfo<SysConfigResp>> list(SysConfigQueryReq req) {
        PageInfo<SysConfigResp> page = this.sysConfigService.paginationQuery(req);
        return R.ok(page);
    }


    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @ApiOperation(value = "新增")
    @Log(title = "参数配置", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Valid SysConfigAddReq req) {
        boolean result = this.sysConfigService.add(req);
        return super.isSuccess(result);
    }

    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @ApiOperation(value = "编辑")
    @PutMapping
    @Log(title = "参数配置", businessType = BusinessType.UPDATE)
    public R edit(@RequestBody @Valid SysConfigEditReq req) {
        boolean result = this.sysConfigService.edit(req);
        return super.isSuccess(result);
    }


    @ApiOperation(value = "查询详情")
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{id}")
    public R<SysConfigResp> findById(@PathVariable(value = "id") Long id) {
        SysConfigResp resp = this.sysConfigService.findById(id);
        return R.ok(resp);
    }

    /**
     * localhost:8080/admin/config/1,2,3,4
     *
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/{ids}")
    @Log(title = "参数配置", businessType = BusinessType.DELETE)
    public R<String> deleteById(@PathVariable(value = "ids") Long[] ids) {
        boolean result = this.sysConfigService.deleteById(ids);
        return super.isSuccess(result);
    }


    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @ApiOperation(value = "导出到Excel")
    @PostMapping("/export")
    public void download(HttpServletResponse response, SysConfigQueryReq req, Long[] ids) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        // .xlsx 文件的  mine-type类型
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //内容响应编码
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("sysconfig", "UTF-8").replaceAll("\\+", "%20");
        //告诉浏览器，文件名称叫什么
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        //查询数据库，向EasyExcel写入数据
        List<SysConfigResp> data = this.sysConfigService.export(req, ids);

        EasyExcel.write(response.getOutputStream(), SysConfigResp.class).sheet().doWrite(data);
    }

}
